//
//  mainViewController.swift
//  weiboxiangmu
//
//  Created by wei_jingyun on 15/6/23.
//  Copyright © 2015年 wei_jingyun. All rights reserved.
//

import UIKit

class mainViewController: UITabBarController  {
    // 全局属性定义
    private  let ba = mainTabBar()

    override func viewDidLoad() {
      super.viewDidLoad()
        /// viewDidLoad 时并不会加载只有显示时才会懒加载 可在这进行替换
        /// tabBar 是只读属性但是可以使用 KVC大招强行设置 设置自定义tabBar 覆盖一个 View并不太好
        setValue(ba, forKey: "tabBar")
        addChildViewController()
        
        /// 添加按钮监听  因为触发调用 是 mainTabBar 中的 控件 所以不能让触发方法为私有
        ba.commBut.addTarget(self, action: "baButClick:", forControlEvents:UIControlEvents.TouchUpInside)
        
    }
    

    //MARK: 发送微博
    func baButClick(but : UIButton){
        if sharedUserAccount == nil {
            NetWorkTools.sharedNetWorkTools().showLogin()
            return
        }
        let nav = UINavigationController(rootViewController: ComposeViewController())
        presentViewController(nav, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    private  func addChildViewController() {
        
         /// 读取json
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)!
        let data = NSData(contentsOfFile: path)!
        
        do{
//            let array = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
            let array = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
            
            // 遍历数组时必须指明 数组内对象的格式 [格式]
            for dict in array as! [[String : String]]{
                addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
            }
            
        } catch{
            // 直接捕获错误，解析错误就直接进行绝对正确的代码
//            Debug.Log(error)
            
            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
            addChildViewController("DisoverTableViewController", title: "发现", imageName: "tabbar_message_center")
            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_discover")
            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }
        
    }
    
    /// 添加控制器 private  私有 重载  加 ? 才允许传nil
    private func addChildViewController(vcName : String , title : String , imageName : String) {
        /// 命名空间 使用名称
        let nameSpace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        let clsName = nameSpace + "." + vcName
//        Debug.Log(clsName)
        let cls: AnyClass = NSClassFromString(clsName)!
        
        // 注意 如果使用alloc() 创建是不会初始化变量的
        let vc = cls.new() as! UITableViewController
        
        tabBar.tintColor = UIColor.orangeColor()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }
}
